package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面,实现公共字段自动填充处理逻辑
 */
@Slf4j
@Aspect
@Component
public class AutoFillAspect {

    /**
     * 提取切点表达式
     */
    @Pointcut("execution(* com.sky.mapper.admin.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {
    }

    /**
     * 前置通知增强
     *
     * @param joinpoint
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinpoint) {
        log.info("开始进行公共字段自动填充,前置通知增强");
        // 获取方法签名
        MethodSignature signature = (MethodSignature) joinpoint.getSignature();
        // 获取方法上的注解
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        // 获取被拦截方法上的数据库操作类型
        OperationType operationType = annotation.value();

        //获取到当前被拦截的方法的参数--实体对象
        Object[] args = joinpoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object arg = args[0];
        Long currentId = BaseContext.getCurrentId();
        LocalDateTime now = LocalDateTime.now();
        // 判断是新增还是修改
        if (operationType == OperationType.INSERT) {
            try {
                Method setCreateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setCreateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                // 通过反射为对象属性赋值
                setCreateUser.invoke(arg, currentId);
                setCreateTime.invoke(arg, now);
                setUpdateUser.invoke(arg, currentId);
                setUpdateTime.invoke(arg, now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                Method setUpdateUser = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = arg.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                // 通过反射为对象属性赋值
                setUpdateUser.invoke(arg, currentId);
                setUpdateTime.invoke(arg, now);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
